package net.droiding.server.parser;

import net.droiding.server.pojo.ProviderKey;
import net.droiding.server.pojo.Station;
import net.droiding.server.pojo.StationData;

public class MxpParser extends AbstractKmlParser {
	@Override
	protected Station createStation(String description, String coordinates) {
		String address = null;
		int bikes = 0;
		int slots = 0;
		double latitude = 0d;
		double longitude = 0d;
		
		if (description != null && coordinates != null) {
			String first_piece = "<div style=\"margin:10px\"><div style=\"font:bold 11px verdana;color:#cf152c;margin-bottom:10px\">";
			String second_piece = "<div style=\"margin-left:5px;float:left;font:bold 11px verdana;color:green\">";
			int start_addr = description.indexOf(first_piece) + first_piece.length() + 1;
			int end_addr = description.indexOf("</div>");
			address = description.substring(start_addr, end_addr).toUpperCase();
			address = address.replace("\\", "");
			String remaining = description.substring(end_addr + 6);
			int start_ava = remaining.indexOf(second_piece) + second_piece.length();
			int end_ava = remaining.indexOf("<br />", start_ava);
			String bikes_availability = remaining.substring(start_ava, end_ava);
			remaining = remaining.substring(end_ava + "<br />".length());
			String slot_availability = remaining.substring(0, remaining.indexOf("<br />"));
			try {
				bikes = Integer.parseInt(bikes_availability);
				slots = Integer.parseInt(slot_availability);
			} catch (Throwable _ignore) {}
			try {
				String[] coordsArray = coordinates.split(",");
				String lng = coordsArray[0];
				String lat = coordsArray[1];
				
				latitude = Double.parseDouble(lat);
				longitude = Double.parseDouble(lng);
			} catch (Throwable _ignore) {
				return null;
			}
			
			if (address != null && !"".equals(address)) {
				String key = address;
				String location = address;
				return new Station(key, ProviderKey.MXP, location, latitude, longitude, new StationData(key, ProviderKey.MXP, bikes, slots, true, System.currentTimeMillis()));				
			}
		}
		return null;
	}
}
